﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using NGE;

namespace MH.SE
{
    public partial class EntryInfo : UserControl
    {
        public EntryInfo()
        {
            InitializeComponent();
        }
        Game game;
        Rectangle zone;
        public Rectangle Zone
        {
            set
            {
                zone = value;
                refresh();
            }
        }

        public void LoadScene(Game g)
        {
            game = g;
            refresh2();
            refresh();
        }
        public void refresh2()
        {
            comboBox2.Text = "";
            comboBox2.Items.Clear();
            for (int i = 0; i < ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer.Count; i++)
            {
                comboBox2.Items.Add(((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[i].ToSceneFileName);
            }
            comboBox2.SelectedIndex = comboBox2.Items.Count - 1;
        }
        public void refresh()
        {
            try
            {
                int j = comboBox2.SelectedIndex;
                int w = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.TileSize.Width;
                int h = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.TileSize.Height;

                textBox4.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].Zone.X / w + "";
                textBox5.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].Zone.Y / h + "";
                textBox3.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].Zone.Width / w + "";
                textBox2.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].Zone.Height / h + "";


                textBox1.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].ToSceneFileName;
                textBox9.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].DefaultLocation.X + "";
                textBox8.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].DefaultLocation.Y + "";
                comboBox1.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j].DefaultDirection.ToString();
            }
            catch { }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                return;
            }
            EntryCode ec = new EntryCode();
            ec.DefaultDirection = (Direction)comboBox1.SelectedIndex;
            ec.DefaultLocation = new Microsoft.DirectX.Vector3(int.Parse(textBox9.Text), int.Parse(textBox8.Text), 0);
            ec.ToSceneFileName = textBox1.Text;
            ec.Zone = zone;
            if (((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer == null)
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer = new EntryCodeLayer();
            }
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[ec.Zone.X, ec.Zone.Y] = ec;

            refresh2();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int j = comboBox2.SelectedIndex;
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer[j] = null;
            comboBox2.Text = "";
            refresh2();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.EntryCodeLayer = new EntryCodeLayer();
        }
    }
}
